#!/bin/sh

if [ $# -lt 1 ]; then
    echo "Error==>Need args,eg fxcore.x64.mt.25581.1599"
    exit 1
fi

FXCORE_VERSION=$1
FXCORE_SPEC_NAME=$1.spec

RRR_DEFAULT_OUTPUT_FOLDER_NAME=output
if [ -n "$2" ]; then
 echo "#2 value: $2"
 CUR_OUTPUT_FOLDER=../$2/
else
 echo "#2 value null,used default"
 CUR_OUTPUT_FOLDER=../$RRR_DEFAULT_OUTPUT_FOLDER_NAME/
fi

#CUR_SPECS_FOLDER=../specs/
#CUR_SPECS_PATH=$CUR_SPECS_FOLDER$FXCORE_SPEC_NAME

CUR_RPMBUILD_FOLDER=rpmbuild
CUR_RPMBUILD_SUB_BUILD=BUILD
CUR_RPMBUILD_SUB_RPMS=RPMS
CUR_RPMBUILD_SUB_SOURCES=SOURCES
CUR_RPMBUILD_SUB_SPECS=SPECS
CUR_RPMBUILD_SUB_SRPMS=SRPMS
CUR_RPMBUILD_FOLDER_PATH=../$CUR_RPMBUILD_FOLDER

if [ ! -d "$CUR_RPMBUILD_FOLDER_PATH" ]; then
    echo "No rpmfolder: $CUR_RPMBUILD_FOLDER_PATH,make it now"
    mkdir $CUR_RPMBUILD_FOLDER_PATH
    if [ $? -ne 0 ]; then
      echo "Error==>make folder: $CUR_RPMBUILD_FOLDER_PATH failure"
      exit 1
    fi
fi
echo "Find $CUR_RPMBUILD_FOLDER_PATH success"
mkdir -p $CUR_RPMBUILD_FOLDER_PATH/{$CUR_RPMBUILD_SUB_BUILD,$CUR_RPMBUILD_SUB_RPMS,$CUR_RPMBUILD_SUB_SOURCES,$CUR_RPMBUILD_SUB_SPECS,$CUR_RPMBUILD_SUB_SRPMS}
if [ $? -ne 0 ]; then
      echo "Error==>make folder: $CUR_RPMBUILD_FOLDER_PATH all subfolders failure"
      exit 1
    fi
echo "Make $CUR_RPMBUILD_FOLDER_PATH all subfolders success"

FXCORE_SRC_FOLDER=../fxcore
FXCORE_SRC_TAG_GZ_NAME=fxcore.tar.gz
FXCORE_SRC_FILE=$FXCORE_SRC_FOLDER/$FXCORE_SRC_TAG_GZ_NAME

USR_RPMBUILD_SRC_FOLDER=../$CUR_RPMBUILD_FOLDER
USR_RPMBUILD_ROOT=~/
USR_RPMBUILD_SPECS=$USR_RPMBUILD_ROOT$CUR_RPMBUILD_FOLDER/$CUR_RPMBUILD_SUB_SPECS
USR_RPMBUILD_SOURCES=$USR_RPMBUILD_ROOT$CUR_RPMBUILD_FOLDER/$CUR_RPMBUILD_SUB_SOURCES
USR_RPMBUILD_RPMS=$USR_RPMBUILD_ROOT$CUR_RPMBUILD_FOLDER/$CUR_RPMBUILD_SUB_RPMS

echo "rpmbuild fxcore version is: $1"
echo "destination fxcore spec file name is: $FXCORE_SPEC_NAME"

#spec begin
# create new fxcore spec
CUR_SPECS_PATH=$CUR_RPMBUILD_FOLDER_PATH/$CUR_RPMBUILD_SUB_SPECS/$FXCORE_SPEC_NAME
SPEC_BUILD_SH=./spec.sh

if [ ! -f $SPEC_BUILD_SH ]; then
   echo "Error==>No find spec shell: $SPEC_BUILD_SH"
   exit 1
fi

if [ ! -x $SPEC_BUILD_SH ]; then
   echo "Info==>No x permission: $SPEC_BUILD_SH"
   chmod +x $SPEC_BUILD_SH
else
   echo "Info=>Has x permission: $SPEC_BUILD_SH"
fi

$SPEC_BUILD_SH $FXCORE_VERSION $CUR_SPECS_PATH

if [ ! -f $CUR_SPECS_PATH ]; then
   echo "Error==>Create spec file failure: $CUR_SPECS_PATH"
   exit 1
fi
#spec end

which rpmbuild
if [ $? -ne 0 ]; then
    echo "Warning==>Need rpmbuild,try to install"
    yum -y install rpm-build
    if [ $? -ne 0 ]; then
        echo "Error==>Install rpm-build failure,check permission or others"
        exit 1
    fi
    yum -y install redhat-rpm-config
    if [ $? -ne 0 ]; then
        echo "Error==>Install redhat-rpm-config failure,check permission or others"
        exit 1
    fi
fi
echo "Check rpmbuild installed"

USR_HOME_SOURCE_RPMBUILD=$USR_RPMBUILD_ROOT$CUR_RPMBUILD_FOLDER
USR_HOME_SOURCE_RPMBUILD_BAK=$USR_HOME_SOURCE_RPMBUILD.bak
if [ -d "$USR_HOME_SOURCE_RPMBUILD" ]; then
    echo "Info===>Find user's old rpmbuild,backup it now"
    mv $USR_HOME_SOURCE_RPMBUILD $USR_HOME_SOURCE_RPMBUILD_BAK
   if [ $? -ne 0 ]; then
    echo "Error==>Backup user's old rpmbuild folder failure,delete it now"
    rm -rf $USR_HOME_SOURCE_RPMBUILD
   fi
fi

cp -Rf $USR_RPMBUILD_SRC_FOLDER $USR_RPMBUILD_ROOT
if [ $? -ne 0 ]; then
    echo "Error==>copy rpmbuild folder to user's home failure"
    exit 1
fi
echo "Copy rpmbuild to user's home success"

if [ ! -d "$FXCORE_SRC_FOLDER" ]; then
    echo "Error==>Not find fxcore path: $FXCORE_SRC_FOLDER"
    exit 1
fi
echo "Find $FXCORE_SRC_FOLDER success..."

if [ ! -f "$FXCORE_SRC_FILE" ]; then
    echo "Error==>Not find fxcore.tar.gz path: $FXCORE_SRC_FILE"
    exit 1
fi
echo "Find $FXCORE_SRC_FILE success"

cp -rf $FXCORE_SRC_FILE $USR_RPMBUILD_SOURCES/$FXCORE_SRC_TAG_GZ_NAME
if [ $? -ne 0 ]; then
    echo "Error==>copy fxcore tar.gz :$FXCORE_SRC_FILE to $USR_RPMBUILD_SOURCES/$FXCORE_SRC_TAG_GZ_NAME failure...."
    exit 1
fi
echo "Copy $FXCORE_SRC_TAG_GZ_NAME success"



if [ -f "$CUR_SPECS_PATH" ]; then
    cp -rf $CUR_SPECS_PATH $USR_RPMBUILD_SPECS/$FXCORE_SPEC_NAME
    if [ $? -ne 0 ]; then
        echo "Error==>copy fxcore spec : $CUR_SPECS_PATH to $USR_RPMBUILD_SPECS/$FXCORE_SPEC_NAME failure...."
        exit 1
    fi
    rpmbuild -bb $USR_RPMBUILD_SPECS/$FXCORE_SPEC_NAME
else
    echo "Error==>Not find fxcore destination spec: $CUR_SPECS_PATH,check it please"
    exit 1
fi

if [ -f "$USR_RPMBUILD_SPECS/$FXCORE_SPEC_NAME" ]; then
    rpmbuild -bb $USR_RPMBUILD_SPECS/$FXCORE_SPEC_NAME
else
    echo "Error==>Not find fxcore destination spec: $USR_RPMBUILD_SPECS/$FXCORE_SPEC_NAME,check it please"
    exit 1
fi

Machine_BITS_UNAME_M=`uname -m`
if  [ ! -n "$Machine_BITS_UNAME_M" ] ;then
    Machine_BITS_UNAME_I=`uname -i`
    if  [ ! -n "$Machine_BITS_UNAME_I" ] ;then
        echo "Can not obtain machine bits"
    else
        echo $Machine_BITS_UNAME_I
        RPM_PKG_FINAL_FOLDER_PATH=$USR_RPMBUILD_RPMS/$Machine_BITS_UNAME_I
    fi
else
    echo $Machine_BITS_UNAME_M
    RPM_PKG_FINAL_FOLDER_PATH=$USR_RPMBUILD_RPMS/$Machine_BITS_UNAME_M
fi

if  [ -n "$RPM_PKG_FINAL_FOLDER_PATH" ] ;then
    echo "RPM_PKG_FINAL_FOLDER_PATH find value:$RPM_PKG_FINAL_FOLDER_PATH"
    FX_TAR_GZ_FINIAL_PATH_GREP_NAME=`ls $RPM_PKG_FINAL_FOLDER_PATH | grep $FXCORE_VERSION`
    echo "grep rpm..result:$FX_TAR_GZ_FINIAL_PATH_GREP_NAME"
    if  [ -n "$FX_TAR_GZ_FINIAL_PATH_GREP_NAME" ] ;then
        FX_TAR_GZ_FINIAL_PATH=$RPM_PKG_FINAL_FOLDER_PATH/$FX_TAR_GZ_FINIAL_PATH_GREP_NAME
        echo "FX_TAR_GZ_FINIAL_PATH find value:$FX_TAR_GZ_FINIAL_PATH"
    fi
fi

echo "Finish fxcore RPM making successfully"
if  [ -f "$FX_TAR_GZ_FINIAL_PATH" ] ;then
    echo "Fetch fxcore rpm in folder:$FX_TAR_GZ_FINIAL_PATH"
else
   if  [ -d "$RPM_PKG_FINAL_FOLDER_PATH" ] ;then
       echo "Fetch fxcore rpm in folder:$RPM_PKG_FINAL_FOLDER_PATH"
   fi
fi

if [ ! -d "$CUR_OUTPUT_FOLDER" ]; then
    echo "Info===>Output folder not exist,make it now: $CUR_OUTPUT_FOLDER"
    mkdir $CUR_OUTPUT_FOLDER
    if [ $? -ne 0 ]; then
        echo "Info===>Make directory: $CUR_OUTPUT_FOLDER failure,fetch it manually please"
        exit 1
    fi
fi

if [ -f "$FX_TAR_GZ_FINIAL_PATH" ]; then
   if  [ -n "$FX_TAR_GZ_FINIAL_PATH_GREP_NAME" ] ;then
     CUR_OUTPUT_FX_RPM_FINAL_PATH=$CUR_OUTPUT_FOLDER$FX_TAR_GZ_FINIAL_PATH_GREP_NAME
     echo "user's output path:$CUR_OUTPUT_FX_RPM_FINAL_PATH"
     cp -rf $FX_TAR_GZ_FINIAL_PATH $CUR_OUTPUT_FX_RPM_FINAL_PATH
     if [ $? -ne 0 ]; then
       echo "Copy rpm from user's home to output success: $FX_TAR_GZ_FINIAL_PATH -> $CUR_OUTPUT_FX_RPM_FINAL_PATH"
     else
       echo "Copy rpm from user's home to output failure: $FX_TAR_GZ_FINIAL_PATH -> $CUR_OUTPUT_FX_RPM_FINAL_PATH"
     fi
   else
     cp -rf $FX_TAR_GZ_FINIAL_PATH $CUR_OUTPUT_FOLDER
     if [ $? -ne 0 ]; then
       echo "Copy rpm from user's home to output folder success: $FX_TAR_GZ_FINIAL_PATH -> $CUR_OUTPUT_FX_RPM_FINAL_PATH"
     else
       echo "Copy rpm from user's home to output folder failure: $FX_TAR_GZ_FINIAL_PATH -> $CUR_OUTPUT_FX_RPM_FINAL_PATH"
     fi
   fi
else
   if  [ -n "$RPM_PKG_FINAL_FOLDER_PATH" ] ;then
     echo "Can not validate your machine's bits,please get rpm manually from: $RPM_PKG_FINAL_FOLDER_PATH"
     exit 1
   else
     echo "Can not validate your machine's hardware information,please get rpm manually from: $USR_RPMBUILD_RPMS"
     exit 1
   fi
fi


if [ -f "$RPM_PKG_FINAL_FOLDER_PATH" ]; then
    echo "Fetch fxcore rpm in path: $RPM_PKG_FINAL_FOLDER_PATH"
fi


if  [ -n "$USR_HOME_SOURCE_RPMBUILD_BAK" ] ;then
    if [ -d "$USR_HOME_SOURCE_RPMBUILD_BAK" ]; then
      echo "Prepare restore backup rpmbuild folder: $USR_HOME_SOURCE_RPMBUILD_BAK"
      if [ -d "$USR_HOME_SOURCE_RPMBUILD" ];then
        echo "remove temp rpmbuild folder: $USR_HOME_SOURCE_RPMBUILD"
        rm -rf $USR_HOME_SOURCE_RPMBUILD
      fi
      echo "restore source rpmbuild folder: $USR_HOME_SOURCE_RPMBUILD_BAK -> $USR_HOME_SOURCE_RPMBUILD"
      mv $USR_HOME_SOURCE_RPMBUILD_BAK $USR_HOME_SOURCE_RPMBUILD
    fi
fi


if [ -d "$CUR_RPMBUILD_FOLDER_PATH" ];then
   echo "Delete temp rpmbuild: $CUR_RPMBUILD_FOLDER_PATH"
   rm -rf $CUR_RPMBUILD_FOLDER_PATH
fi

if [ -f "$CUR_OUTPUT_FX_RPM_FINAL_PATH" ]; then
    echo "Please fetch fxcore rpm in path: $CUR_OUTPUT_FX_RPM_FINAL_PATH"
else
    echo "Please fetch fxcore rpm in folder: $CUR_OUTPUT_FOLDER"
fi


echo "3Q,Complete all RPM actions now"
exit 0


